package com.timesheet.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.base.common.mapper.JsonMapper;
import com.base.common.utils.DateUtils;
import com.company.model.CompanyProject;
import com.company.model.ProjectWbs;
import com.company.service.ProjectManageService;
import com.company.service.ProjectWbsService;
import com.timesheet.constants.AuditState;
import com.timesheet.model.Timesheet;
import com.timesheet.service.TimesheetService;

@Controller
public class TimesheetController {
	
	@Autowired
	private ProjectManageService projectManagerServer;
	@Autowired
	private ProjectWbsService projectWbsService;
	@Autowired
	private TimesheetService timesheetService;

	@RequestMapping(value = "/timesheet", method = RequestMethod.POST)
	public ModelAndView renderTimeSheetView(HttpSession httpSession) throws Exception {
		ModelAndView mv = new ModelAndView("/timesheet/timesheets");
		
		String companyId = httpSession.getAttribute("companyId").toString();
		
		List<CompanyProject> projectByCompanyList = projectManagerServer.findProjectByCompanyId(companyId);
		
		SAXReader reader = new SAXReader();
		Document document = reader.read(this.getClass().getResourceAsStream("/timesheet/specialDays.xml"));
		
		Element root = document.getRootElement();
		
		String holidays = root.elementText("Holidays");
		String workdays = root.elementText("Workdays");
		
		mv.addObject("holidays", holidays);
		mv.addObject("workdays", workdays);
		mv.addObject("projects", projectByCompanyList);
		mv.addObject("nowDate", DateUtils.getDate());
		
		return mv;
	}
	
	@RequestMapping(value = "/timesheet/loadWps", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectWbs> loadWps(String projectId) throws Exception {
		
		List<ProjectWbs> wpsList = projectWbsService.findProjectWbsByProjectId(projectId);
		
		return wpsList;
	}
	
	@RequestMapping(value = "/timesheet/save", method = RequestMethod.POST)
	@ResponseBody
	public String saveTimesheet(String timesheetJson, HttpSession httpSession) {
		
		List<Timesheet> timesheets = JSONArray.parseArray(timesheetJson, Timesheet.class);
		int timesheetNumOfMon= 0;
		int timesheetNumOfTues = 0;
		int timesheetNumOfWed = 0;
		int timesheetNumOfThur = 0;
		int timesheetNumOfFri = 0;
		int timesheetNumOfSat = 0;
		int timesheetNumOfSun = 0;
		String validMsg = null;
		for (Timesheet timesheet : timesheets) {
			if (timesheet.getFifthDay() != null) {
				timesheetNumOfMon += timesheet.getFifthDay();
			}
			if (timesheet.getSecondDay() != null) {
				timesheetNumOfTues += timesheet.getSecondDay();
			}
			if (timesheet.getThirdDay() != null) {
				timesheetNumOfWed += timesheet.getThirdDay();
			}
			if (timesheet.getFourthDay() != null) {
				timesheetNumOfThur += timesheet.getFourthDay();
			}
			if (timesheet.getFifthDay() != null) {
				timesheetNumOfFri += timesheet.getFifthDay();
			}
			if (timesheet.getSixthDay() != null) {
				timesheetNumOfSat += timesheet.getSixthDay();
			}
			if (timesheet.getSeventhDay() != null) {
				timesheetNumOfSun += timesheet.getSeventhDay();
			}
			timesheet.setUserId(httpSession.getAttribute("userId").toString());
			timesheet.setAuditState(AuditState.SUBMITED.getValue());
		}
		if (timesheetNumOfMon > 8 
				|| timesheetNumOfTues > 8
				|| timesheetNumOfWed > 8
				|| timesheetNumOfThur > 8
				|| timesheetNumOfFri > 8
				|| timesheetNumOfSat > 8
				|| timesheetNumOfSun > 8) {
			validMsg = "一天累计工时不能超过 8 小时";
		}
		timesheetService.saveTimesheet(timesheets);
		
		return validMsg;
	}
	
	@RequestMapping(value = "/timesheet/specialWeekTimesheet", method = RequestMethod.POST)
	@ResponseBody
	public List<Timesheet> getSpecialWeekTimesheet(String startDate, String endDate, HttpSession httpSession) throws Exception {
		Timesheet timesheet = new Timesheet();
		timesheet.setUserId(httpSession.getAttribute("userId").toString());
		timesheet.setStartDate(DateUtils.parseDate(startDate, "yyyy-MM-dd"));
		timesheet.setEndDate(DateUtils.parseDate(endDate, "yyyy-MM-dd"));
		timesheet.setAuditState(AuditState.SUBMITED.getValue());
		
		List<Timesheet> list = timesheetService.findByDateAndUser(timesheet);
		return list;
	}
}
